?><?
class cls_event_log_item extends cls_entity_item{
  function entity(){return "event_log";}

  function data_fields(){
    return array(
      "event_log_id",
      "event_log_created",
      "event_log_updated",
      "event_log_control",
      "event_log_package",
      "event_log_changes",
      "event_log_operation",
      "entity_id",
      "entity_name",
      "entity_value_name",
      "user_id",
      "visitor_id",
    );
  }

  function init_fields(){
    $user=get_user();
    $user_id=$user->id;
    $visitor_id=get_visitor();
    $fields=array(
      "user_id"=>$user_id,
      "visitor_id"=>$visitor_id,
    );
    return $fields;
  }

  function insert_control($control,$entity_item=false){
    $user=get_user();
    $user_id=$user->id;
    $visitor_id=get_visitor();
    $fields=array(
      "entity_name"=>cls_event_log_item::get_entity_name($entity_item),
      "entity_id"=>cls_event_log_item::get_entity_id($entity_item),
      "event_log_control"=>$control->action,
      "event_log_package"=>$control->package,
      "user_id"=>$user_id,
      "visitor_id"=>$visitor_id,
    );
    return parent::insert(cls_event_log::entity(),$fields);
  }

  function get_entity_name($entity_item){
    if(is_object($entity_item)){
      return $entity_item->entity();
    }else{
      return "";
    }
  }

  function get_entity_id($entity_item){
    if(is_object($entity_item)){
      return $entity_item->id();
    }else{
      return 0;
    }
  }

  function insert_action($action){
    $user=get_user();
    $user_id=$user->id;
    $visitor_id=get_visitor();
    $fields=array(
      "entity_name"=>cls_event_log_item::get_entity_name($entity_item),
      "entity_id"=>cls_event_log_item::get_entity_id($entity_item),
      "event_log_old"=>$old_value,
      "event_log_new"=>$new_value,
      "user_id"=>$user_id,
      "visitor_id"=>$visitor_id,
    );
    return parent::insert(cls_event_log::entity(),$fields);
  }

  function insert_entity($entity,$id,$operation,$changes=false){
    if($entity!==cls_event_log::entity()){
      $user=get_user();
      $user_id=$user->id;
      $visitor_id=get_visitor();
      $fields=array(
        "entity_name"=>$entity,
        "entity_id"=>$id,
        "event_log_operation"=>$operation,
        "user_id"=>$user_id,
        "visitor_id"=>$visitor_id,
      );      
      if($changes){$fields["event_log_changes"]=serialize($changes);}
      return parent::insert(cls_event_log::entity(),$fields);
    }
  }

  function insert_log($entity_item,$old_value,$new_value){  
    $user=get_user();
    $user_id=$user->id;
    $visitor_id=get_visitor();
    $fields=array(
      "entity_name"=>cls_event_log_item::get_entity_name($entity_item),
      "entity_id"=>cls_event_log_item::get_entity_id($entity_item),
      "event_log_old"=>$old_value,
      "event_log_new"=>$new_value,
      "user_id"=>$user_id,
      "visitor_id"=>$visitor_id,
    );
    return parent::insert(cls_event_log::entity(),$fields);
  }

}